package com.yunding.house.controller;


import com.yunding.OA.pojo.Ydjf;
import com.yunding.OA.service.YdjfOaservice;
import com.yunding.house.pojo.Phonecode;
import com.yunding.house.pojo.User;
import com.yunding.house.pojo.Wxuser;
import com.yunding.house.service.IWxuserService;
import com.yunding.house.service.PhonecodeService;
import com.yunding.house.service.UserService;
import com.yunding.house.utils.R;
import com.yunding.house.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 用户登录账户表 前端控制器
 * </p>
 *
 * @author linyang
 * @since 2019-08-15
 */
@Controller
//@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private IWxuserService iWxuserService;
    @Autowired
    private PhonecodeService phonecodeService;
    @Autowired
    private YdjfOaservice ydjfOaservice;


    @RequestMapping("/register")
    public String register(String username,
                           String password,
                           String phone,
                           String code,
                           String invitationCode) {
        System.out.println("邀请码正确");

        //传入 账号  密码 电话  验证码  邀请码
        //1.判断号码
        Phonecode phonecode = phonecodeService.IsSendCodephoneInfo(phone.trim());
        System.out.println("phonecode:=" + phonecode.toString());
        //2.根据openid获取对象
        String openid = "123";
        Wxuser wxuser = iWxuserService.findByOpenId(openid);
        User user = new User();
        //oa账号
        Ydjf loginName = ydjfOaservice.selectByloginName(username);
        if (loginName == null) {
            //3.数据库查邀请码是否存在
            User usercode = userService.selectByinvcode(invitationCode.trim());
            if (usercode == null) {
                System.out.println("邀请码错误");
                return StatusCode.INVITATIONCODE;
            } else if (!(phonecode.getCode().equals(code) || phonecode.getCode() == code)) {
                System.out.println("手机验证码不相等");
                return StatusCode.CODE;
            }else {
                user.setUsername(username);user.setPassword(password);
                user.setName(wxuser.getNickName());user.setAvatar(wxuser.getHeadimgurl());
                user.setCreatetime(wxuser.getUpdateTime());user.setPhone(phone);
                user.setEnable(true);user.setSex(wxuser.getSex());
                user.setOpenid(openid);
                Boolean boo = userService.save(user);
                if(boo){
                    //跳转到首页
//                    return  "forward:select.do?code=200&status=ok!";
                    return  "a";
                }else {
                    return StatusCode.FAIL;
                }
            }
        }else {
            user.setUsername(username);user.setPassword(password);
            user.setName(wxuser.getNickName());user.setAvatar(wxuser.getHeadimgurl());
            user.setCreatetime(wxuser.getUpdateTime());user.setPhone(phone);
            user.setEnable(true);user.setSex(wxuser.getSex());
            user.setOpenid(openid);
            System.out.println("进入");
            Boolean boo = userService.save(user);
            if(boo){
                //跳转到首页
//                    return  "forward:select.do?code=200&status=ok!";
                System.out.println("ok");
                return  "a";
            }else {
                return StatusCode.FAIL;
            }
        }

    }
}


